﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Knlght.Web.Infrastructure
{
    public static class PageHepler
    {
        /// <summary>
        /// 生成Bootstrap可识别的分页HTML
        /// </summary>
        /// <param name="getUrlExec">基础页面地址</param>
        /// <param name="currentIndex">当前页号</param>
        /// <param name="totalPages">总页数</param>
        /// <param name="totalNavCount">导航条显示总数</param>
        public static string RenderBootstrapPagingHtml(Func<int, string> getUrlExec, int currentIndex, int totalPages, int totalNavCount = 7)
        {
            if (getUrlExec == null)
                throw new ArgumentNullException("getUrlExec");

            if (currentIndex < 1) currentIndex = 1;
            if (currentIndex > totalPages)
                return string.Empty;

            StringBuilder sb = new StringBuilder();
            sb.Append("<nav class='page-list'>");
            sb.Append("<ul class='pagination'>");
            //第一页
            if (currentIndex <= 1)
            {
                sb.Append("<li class='disabled'>");
                sb.Append("<a href = '#' >");
                sb.Append("<span class='glyphicon glyphicon-step-backward'></span>");
                sb.Append("</a>");
                sb.Append("</li>");
            }
            else
            {
                sb.Append("<li>");
                sb.AppendFormat("<a href = '{0}' >", getUrlExec(1));
                sb.Append("<span class='glyphicon glyphicon-step-backward'></span>");
                sb.Append("</a>");
                sb.Append("</li>");
            }
            //上一页
            if (currentIndex <= 1)
            {
                sb.Append("<li class='disabled'>");
                sb.Append("<a href = '#' >");
                sb.Append("<span>&lt;</span>");
                sb.Append("</a>");
                sb.Append("</li>");

            }
            else
            {
                sb.Append("<li>");
                sb.AppendFormat("<a href = '{0}' >", getUrlExec(currentIndex - 1));
                sb.Append("<span>&lt;</span>");
                sb.Append("</a>");
                sb.Append("</li>");
            }

            if (totalNavCount <= 1)
            {
                sb.AppendFormat("<span class='current'>{0}</span>", currentIndex);
            }
            else
            {
                var unitCount = totalNavCount / 2;

                if (totalNavCount % 2 == 0)
                {
                    totalNavCount++;
                }

                //取出中间值

                int beginIndex, endIndex;

                beginIndex = currentIndex - unitCount;

                if (beginIndex < 1)
                {
                    //当前值减半之后小于1
                    beginIndex = 1;
                    endIndex = totalNavCount;
                }
                else if ((beginIndex + totalNavCount - 1) > totalPages)
                {
                    //当前值减半之后+显示页号数>总页数
                    beginIndex = totalPages - totalNavCount + 1;
                    if (beginIndex < 1)
                        beginIndex = 1;
                    endIndex = totalPages + (currentIndex - 1);
                }
                else
                {
                    endIndex = currentIndex + unitCount;
                }

                if (endIndex > totalPages)
                {
                    endIndex = totalPages;
                }

                for (int i = beginIndex; i <= endIndex; i++)
                {
                    if (i == currentIndex)
                    {
                        sb.Append("<li class='active'>");
                        sb.Append("<a href = '#' >");
                        sb.AppendFormat("<span >{0}</span>", i);
                        sb.Append("</a>");
                        sb.Append("</li>");
                    }
                    else
                    {
                        sb.Append("<li>");
                        sb.AppendFormat("<a href = '{0}' >", getUrlExec(i));
                        sb.AppendFormat("<span>{0}</span>", i);
                        sb.Append("</a>");
                        sb.Append("</li>");
                    }
                }

                if (endIndex < totalPages)
                {
                    sb.Append("<li>");
                    sb.Append("<a href = '#' >");
                    sb.Append("<span>...</span>");
                    sb.Append("</a>");
                    sb.Append("</li>");
                    if (endIndex + 2 <= totalPages)
                    {
                        sb.Append("<li>");
                        sb.AppendFormat("<a href = '{0}' >", getUrlExec(totalPages - 1));
                        sb.AppendFormat("<span>{0}</span>", totalPages - 1);
                        sb.Append("</a>");
                        sb.Append("</li>");

                        sb.Append("<li>");
                        sb.AppendFormat("<a href = '{0}' >", getUrlExec(totalPages));
                        sb.AppendFormat("<span >{0}</span>", totalPages);
                        sb.Append("</a>");
                        sb.Append("</li>");

                    }
                    else
                    {
                        sb.Append("<li>");
                        sb.AppendFormat("<a href = '{0}' >", getUrlExec(totalPages));
                        sb.AppendFormat("<span>{0}</span>", totalPages);
                        sb.Append("</a>");
                        sb.Append("</li>");
                    }
                }
            }
            //下一页
            if (currentIndex >= totalPages)
            {
                sb.Append("<li class='disabled'>");
                sb.Append("<a href = '#'>");
                sb.Append("<span>&gt;</span>");
                sb.Append("</a>");
                sb.Append("</li>");
            }
            else
            {
                sb.Append("<li>");
                sb.AppendFormat("<a href = '{0}'>", getUrlExec(currentIndex + 1));
                sb.Append("<span>&gt;</span>");
                sb.Append("</a>");
                sb.Append("</li>");
            }
            //最后一页
            if (currentIndex >= totalPages)
            {
                sb.Append("<li class='disabled'>");
                sb.Append("<a href = '#' >");
                sb.Append("<span class='glyphicon glyphicon-step-forward'></span>");
                sb.Append("</a>");
                sb.Append("</li>");
            }
            else
            {
                sb.Append("<li>");
                sb.AppendFormat("<a href = '{0}' >", getUrlExec(totalPages));
                sb.Append("<span class='glyphicon glyphicon-step-forward'></span>");
                sb.Append("</a>");
                sb.Append("</li>");
            }
            sb.Append("</ul>");
            sb.Append("</nav>");

            return sb.ToString();
        }
    }
}
